﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Cn.Edwon.Web.Support;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Web.Session;
using Cn.Edwon.Spring;
using Cn.Edwon.Web;
using Cn.Cellula.Client;

namespace Cn.Cellula.Website.Ws {
    public partial class _Login : PageSupport {
        protected void Page_Load(object sender, EventArgs e) {
            String LoginName = Request.QueryString["u"];
            String Password = Request.QueryString["p"];
            String Cookie = Request.QueryString["cookie"];
            String VilateCode = Request.QueryString["vc"];

            if (!String.IsNullOrEmpty(LoginName) && LoginName.ToLower() == "guest") {
                Response.Clear();
                Response.Write("该用户不存在");
                Response.End();
                return;
            }
            
            if (VilateCode == null) VilateCode = "";
            String bk = "";
            if (Session[ClientBiz.VALIDATE_CODE_SESSION_KEY] != null) {
                String sessCode = (String)Session[ClientBiz.VALIDATE_CODE_SESSION_KEY];
                if (VilateCode.ToLower() != sessCode.ToLower()) {
                    bk = "{\"msg\":\"错误的验证码，请从新输入\",\"url\":\"\"}";
                    Response.Clear();
                    Response.Write(bk);
                    Response.End();
                    return;
                }
            }
            IUserBiz biz = BizFactory.Get<IUserBiz>();
            ISessActor sess = new SessActor();
            new LoginHelper().Login(SiteConfig.Sercurity.UserLoginProperty, LoginName, ClientBiz.Md5(Password), ref sess);
            if (sess != null) {
                if (sess.IsActivated) {//已经激活
                    sess.LastDate = DateTime.Now;
                    sess.LastIP = Request.UserHostAddress;
                    base.Actor = sess;                    
                    if (Cookie == "1" || Cookie == "true" || Cookie == "checked") {
                        ClientBiz.Cookie.Append(sess, Request.UserHostAddress, Response);
                    } else {
                        Request.Cookies.Remove(ClientBiz.Cookie.COOKIE_NAME);
                        ClientBiz.Cookie.AbateCookie(Request, Response);
                    }
                    bk = "{\"msg\":\"\",\"url\":\"" + base.LoginRefUrl + "\"}";
                    Session[ClientBiz.VALIDATE_CODE_SESSION_KEY] = "#";
                    base.LoginRefUrl = null;
                } else {
                    if (SiteConfig.Sercurity.UserRegCheck == SiteConfig.RegisterCheck.Email) {//未激活状态
                        bk = "{\"msg\":\"账户尚未激活，请先激活\",\"url\":\"" + ClientHelper.RootUrl + "Biz/Security/Activate.aspx" + "\"}";
                    } else {//其他认为是未审核通过
                        bk = "{\"msg\":\"账户尚未通过审核\",\"url\":\"\"}";
                    }
                }
            } else {
                bk = "{\"msg\":\""+TxtDef.User.BadUsernameOrPwd+"\",\"url\":\"\"}";
            }
            Response.Clear();
            Response.Write(bk);
            Response.End();
        }
    }
}
